﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Host.Models;

namespace Host.Models
{
    public class FundRules
    {

        /// <summary>
        /// Create the map record for the questiongroup to the question category
        /// </summary>
        /// <param name="questionCategory">QuestionGroup object</param>
        public void CreateMapForCategory(Models.QuestionGroup questionCategory)
        {
            try
            {
                using (Data db = new Data())//use the using statement to automatically clean up reasources
                {
                    db.QuestionGroup.AddObject(questionCategory);//insert the object into the database collection
                    db.SaveChanges();//submit the change to the database
                }
            }
            catch (System.Exception ex)//Catche the exeption
            {
                ExceptionHelper.CreateException(0, ex.Message, 99);//Write the exception into the database
            }
        }

        /// <summary>
        /// Get the QuestionGroups for the Question Category
        /// </summary>
        /// <param name="questionCategory">QuestionCategory</param>
        /// <returns>List of QuestionGroup</returns>
        public List<QuestionGroup> GetQuestionGroupForCategory(Models.QuestionCategory questionCategory)
        {
            try
            {
                using (Data db = new Data())
                {
                    return
                        db.QuestionGroup.Where(i => i.QuestionCategoryId == questionCategory.QuestionCategoryId)
                        .Select(x => new QuestionGroup{ QuestionGroupId = x.QuestionGroupId, Description = x.Description} ).ToList();
                }
            }
            catch (System.Exception ex)
            {
                ExceptionHelper.CreateException(0, ex.Message, 99);//Write the exception into the database
                return null;
            }
        }

        /// <summary>
        /// Save the question into the system
        /// </summary>
        /// <param name="question">Question object</param>
        public void SaveQuestion(Models.Question question)
        {
            try
            {
                using (Data db = new Data())//use the using statement to automatically clean up reasources
                {
                    if (question.AnswerDataTypeId == 6)
                    {
                        question.AnswerFromList = false;
                    }

                    db.Question.AddObject(question);//insert the object into the database collection
                    db.SaveChanges();//submit the change to the database
                }
            }
            catch (System.Exception ex)//Catche the exeption
            {
                ExceptionHelper.CreateException(0, ex.Message, 99);//Write the exception into the database
            }
        }

        public List<Question> GetQuestionForQuestionGroup(Models.QuestionGroup questionGroup)
        {
            try
            {
                using (Data db = new Data())
                {
                    return
                        db.Question.Where(i => i.QuestionGroupId == questionGroup.QuestionGroupId).Select(
                            x => new Question { QuestionId = x.QuestionId, Description = x.Question1}).ToList();
                }
            }
            catch (System.Exception ex)//handle the exception
            {
                ExceptionHelper.CreateException(0, ex.Message, 99);//Write the exception into the database
                return null;//return nothing back
            }
        }

       

        public class QuestionGroup
        {
            public int QuestionGroupId { get; set; }
            public string Description { get; set; }
        }

        public class Question
        {
            public int QuestionId { get; set; }
            public string Description { get; set; }
        }

       
    }
}